package com.rede.googlepaydemo

import android.app.Activity
import android.app.ProgressDialog
import android.os.Bundle
import android.text.TextUtils
import android.util.Log
import android.view.View
import android.widget.Toast
import com.rede.paylib.*
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : Activity(), View.OnClickListener {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        pay_btn.setOnClickListener(this)
        pay_query.setOnClickListener(this)
        buy_query.setOnClickListener(this)

        pay_btn.post {
            initPay()
        }

    }

    //初始化数据
    private fun initPay(){
        val manager = GooglePayManager.get().init(this)
        //注册监听器
        manager.setOnPayListener(object : OnPayListener{
            override fun onPurchasesFinished(data: ArrayList<Product>?) {
                log("pay>>>>onPurchasesFinished>>>>data: $data")
                data?.forEach {
                    //需要消耗掉商品， 否则不能再次购买
                    GooglePayManager.get().consumeAsync(it.token)
                }
            }
            override fun onConsumeFinished(token: String?) {
                log("pay>>>>onConsumeFinished>>>>token: $token")
            }
            override fun onFail(err: String?) {
                log("pay>>>>onFail>>>>$err")
            }
        })
    }

    override fun onClick(v: View) {
        when(v.id){
            R.id.pay_btn->pay()
            R.id.pay_query->queryProdeucDetail()
            R.id.buy_query->queryPurchases()
        }
    }

    private fun queryProdeucDetail(){
        showProcess()
        var list = ArrayList<String>();
        list.add("test_order_01")
        GooglePayManager.get().queryProductDetail(list) { data ->
            desc_txt.text = ""
            if(data!=null&&data.isNotEmpty()){
                data?.forEach{
                    desc_txt.append(it+"\n\n")
                }
            }else{
                desc_txt.text = "未查询到商品数据"
            }
            closeProcess()
        }
    }

    private fun pay(){
        var productId = order_id_et.text.toString()
        if(TextUtils.isEmpty(productId)){
            toast("先输入产品id")
            return
        }
        GooglePayManager.get().buy(this, productId)
    }


    private fun queryPurchases(){
        showProcess()
        GooglePayManager.get().queryPurchases { data ->
            log("queryPurchases>>>>onProducList>>>>data: $data")
            desc_txt.text = ""
            if(data!=null&&data.isNotEmpty()){
                data?.forEach{
                    desc_txt.append("${it.toString()}\n\n")
                }
            }else{
                desc_txt.text = "暂无购买的商品数据"
            }
            closeProcess()
        }
    }

    private var dialog: ProgressDialog?=null
    private fun showProcess() {
        dialog = ProgressDialog(this)
        dialog?.setProgressStyle(android.R.style.Widget_Material_Light_ProgressBar_Large)
        dialog?.setMessage("loading")
        dialog?.setCancelable(false)
        dialog?.show()
    }

    private fun closeProcess(){
        dialog?.cancel()
    }

    private fun log(msg: String?){
        Log.e("AAA", "MainActivity======log======msg:=$msg")
    }
    private fun toast(msg: String){
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show()
    }
}
